import { Schedule } from '../../network/Responses';

export class TrainFilter {
  private CRH_ONLY: boolean = false;// 只看高铁
  private NORMAL_ONLY: boolean = false;// 只看普通车
  private LEFT_TICKET_ONLY: boolean = false;// 只看有票

  private REVIVAL: boolean = false;
  private SMART: boolean = false;
  private MUTE_CABIN: boolean = false;

  // 坐席类别
  private SPECIAL_SEAT: boolean = false;

  private stations: string[] = [];

  public setCrhOnly(v: boolean) {
    this.CRH_ONLY = v;
    if(v)
      this.NORMAL_ONLY = false;
  }

  public setNormalOnly(v: boolean) {
    this.NORMAL_ONLY = v;
    if(v)
      this.CRH_ONLY = false;
  }

  public setLeftTicketOnly(v: boolean) {
    this.LEFT_TICKET_ONLY = v;
  }

  public filter(origin: Schedule[]): Schedule[] {
    const stationCount = this.stations.length;
    let result: Schedule[] = JSON.parse(JSON.stringify(origin));
    if(this.CRH_ONLY)
      result = result.filter(schedule => schedule.trainCode.lastIndexOf("G") != -1 || schedule.trainCode.lastIndexOf("D") != -1);
    if(this.NORMAL_ONLY)
      result = result.filter(schedule => schedule.trainCode.lastIndexOf("G") == -1 && schedule.trainCode.lastIndexOf("D") == -1);
    if(this.LEFT_TICKET_ONLY)
      result = result.filter(schedule => schedule.Classes.special != "无" ||
        (schedule.Classes.first != "无" && schedule.Classes.first != "--") ||
        (schedule.Classes.second != "无" && schedule.Classes.second != "--") ||
        (schedule.Classes.softSleep != "无" && schedule.Classes.softSleep != "--") ||
        (schedule.Classes.hardSleep != "无" && schedule.Classes.hardSleep != "--") ||
        (schedule.Classes.hardSeat != "无" && schedule.Classes.hardSeat != "--") ||
        (schedule.Classes.noSeat != "无" && schedule.Classes.noSeat != "--")
      );
    if(this.REVIVAL)
      result = result.filter(schedule => schedule.TrainType.revival);
    if(this.SMART)
      result = result.filter(schedule => schedule.TrainType.smart);
    if(this.MUTE_CABIN)
      result = result.filter(schedule => schedule.TrainType.muteCabin);
    if(stationCount != 0)
      result = result.filter(schedule => {
        for(let fTime = 0; fTime < stationCount; fTime++) {
          if(schedule.fromStationCode == this.stations[fTime] || schedule.toStationCode == this.stations[fTime])
            return true;
        }
        return false;
      })
    return result;
  }

  public hasFilter(): boolean {
    return this.CRH_ONLY || this.NORMAL_ONLY || this.LEFT_TICKET_ONLY || this.REVIVAL || this.SMART || this.MUTE_CABIN;
  }

  pub
}